<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\AppletPlayletRel;
use App\Model\Playlet;
use App\Model\PlayletEpisodes;
use App\Model\PlayletEpisodesReplace;
use App\Model\PlayletTag;
use App\Service\queue\DramaService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Common\Response\SystemCode;
use App\Service\playlet\PlayletExamineService;

#[AutoController]
class PlayletExamineController  extends BaseController
{

    #[Inject]
    private PlayletExamineService $playletExamineService;
    #[Inject]
    private DramaService $dramaService;
    /**
     * 短剧列表
     */
    public function Index(RequestInterface $request)
    {
        $data = $this->playletExamineService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }

    /**
     * 短剧剧集列表
     */
    public function episodesIndex($id,$type,RequestInterface $request)
    {
        $data = $this->playletExamineService->episodesIndex($id,$type,$request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }

    /**
     * 短剧微信,微信审核接口
     */
    public function AuditDrama(int $id,int $type = 1){

        if(!empty($id)) {
            $playletInfo = Playlet::query()->select(['id','is_finish'])->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }
            if($playletInfo->is_finish == 0){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧未完结不可以上传微信审核');
            }
            $result =  $this->playletExamineService->auditDrama($id,$type);

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '短剧剧目审核提交成功');
            }else{
                return $this->response($result['code'],$result['message'],$result['data']);

            }
        }
    }

    /**
     * 剧集审核
     */
    public function Examine(RequestInterface $request){

        $ids = $request->post('ids');
        $type = $request->post('type') ?? 1;

        if(empty($ids)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $playletEpisodesArray = PlayletEpisodes::query()->whereIn('id', $ids)->get();
            if(empty($playletEpisodesArray)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数不存在');
            }
            //判断上传的ids数目和搜索出来的数目是否一致
            if(count($ids) != count($playletEpisodesArray)){
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧集数ID存在错误');
            }
            $errorMeg = [];
            $successMeg = [];
            $playlet_id = 0;
            foreach ($playletEpisodesArray as $playletEpisodes){
                $playlet_name = '';
                if(!empty($playletEpisodes->playlet)){
                    $playlet_name = $playletEpisodes->playlet->name;
                    $playlet_id = $playletEpisodes->playlet->id;

                    $is_exists = AppletPlayletRel::query()->where(['playlet_id' => $playletEpisodes->playlet->id])->exists();
                    if(!$is_exists){
                        $errorMeg[] = '该集数的短剧没有绑定小程序';
                        continue;
                    }
                }else{
                    $errorMeg[] = '该集数的短剧不存在';
                    continue;
                }
                if($type == 1){
                    //0为无效值；1为审核中；2为最终失败；3为审核通过；4为驳回重填
                    if($playletEpisodes->media_status == 3){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已经审核通过了，请勿反复提交审核';
                        continue;
                    }else if($playletEpisodes->media_status == 1){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已在审核中，请勿反复提交审核';
                        continue;
                    }
                    //内部使用,1:为未提交审核，2:已提交审核，3:审核通过，4:审核失败
                    if($playletEpisodes->is_upload == 2){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已提交排队，请勿反复提交';
                        continue;
                    }
                    if($playletEpisodes->is_upload == 3){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已成功上传，请勿反复提交';
                        continue;
                    }
                }else{
                    if($playletEpisodes->is_dy_upload == 2){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已提交排队，请勿反复提交';
                        continue;
                    }
                    if($playletEpisodes->is_dy_upload == 3){
                        $errorMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数已成功上传，请勿反复提交';
                        continue;
                    }
                }

                $successMeg[] = $playlet_name.':'.$playletEpisodes->playlet_number_name.'集数提交审核排队成功';
            }
            if(!empty($errorMeg)){
                return $this->response(SystemCode::SYSTEM_ERROR_UPLOAD_EPISODES,'提交审核排队失败',$errorMeg);
            }

            if($type == 1){
                PlayletEpisodes::query()->whereIn('id', $ids)->update(['is_upload' => 2,'task_id' => 0]);
            }else{
                PlayletEpisodes::query()->whereIn('id', $ids)->update(['is_dy_upload' => 2,'open_video_id' => 0]);

            }
            //查询
            if(!empty($playlet_id)){
                //查询这部剧是否已经审核通过
                $playletInfo = Playlet::query()->where(['id' => $playlet_id])->first();
                if($type == 1){
                    if($playletInfo->drama_status == 3) {

                        foreach ($ids as $id){
                            //插入
                            PlayletEpisodesReplace::create([
                                'playlet_id' => $playlet_id,
                                'status'    => '1',
                                'playlet_episodes_ids' => $id,
                                'type' => $type
                            ]);
                        }
                    }
                }else if($type == 2){
                    if($playletInfo->album_status == 3) {
                        foreach ($ids as $id){
                            //插入
                            PlayletEpisodesReplace::create([
                                'playlet_id' => $playlet_id,
                                'status'    => '1',
                                'playlet_episodes_ids' => $id,
                                'type' => $type
                            ]);
                        }
                    }
                }
            }
            $this->dramaService->vodPullUpolad($ids,$type);
            return $this->response(SystemCode::SYSTEM_SUCCESS, '提交审核排队成功',$successMeg);
        }

    }
    /**
     * 替换线上的剧集
     */
    public function replaceOnlineDrama(int $id,int $type){

        if(!empty($id)) {
            $playletInfo = Playlet::query()->select(['id'])->where('id', $id)->first();
            if (empty($playletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '短剧不存在');
            }

            $result =  $this->playletExamineService->replaceOnlineDrama($id,$type);

            if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '短剧剧目替换提交成功');
            }else{
                return $this->response($result['code'],$result['message'],$result['data']);

            }
        }
    }
}